package keeto.com.vn.classified.task;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;

import keeto.com.vn.classified.core.ClassifiedCore;
import keeto.com.vn.classified.core.ClassifiedListener;
import keeto.com.vn.classified.network.IProtocol;
import keeto.com.vn.classified.request.GetMyHandBookRequest;
import keeto.com.vn.classified.response.GetMyHandBookResponse;

public class TaskGetMyHandBook extends AbstractTask {
	private String UserName;
	private String AuthKey;
	private int type;
	private int pageNumber;
	private int pageLimit;
	private String UserId;
	private String historyId;


	public TaskGetMyHandBook(ClassifiedListener listener, ClassifiedCore core,
			String userName, String authKey, int type, int pageNumber,
			int pageLimit, String userId, String historyId) {
		super(listener, core);
		UserName = userName;
		AuthKey = authKey;
		this.type = type;
		this.pageNumber = pageNumber;
		this.pageLimit = pageLimit;
		UserId = userId;
		this.historyId = historyId;
	}

	@Override
	public void run() throws ClientProtocolException, JSONException,
			IOException {
		// TODO Auto-generated method stub
		GetMyHandBookRequest request = new GetMyHandBookRequest();
		request.setAuthKey(AuthKey);
		request.setPageLimit(pageLimit);
		request.setPageNumber(pageNumber);
		request.setType(type);
		request.setUserId(UserId);
		request.setUserName(UserName);
		request.setHistoryId(historyId);
		GetMyHandBookResponse response = json.getMyHandBook(request);
		for (ClassifiedListener listener : listeners) {
			listener.finishGetMyhandBook(response.getReturnCode(), response.getListItemEntities(), response.getPageCount(), response.isHasMore());
		}
		
	}

	@Override
	public void timeoutCallback() {
		// TODO Auto-generated method stub
		for (ClassifiedListener l : core.copyAndReconstructListeners(listener)) {
			l.finishGetMyhandBook(IProtocol.STATUS_CODE_CONNECTION_TIMEOUT_502, null,-1 , false);
		}
	}

}
